home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
demos
/
GL
/
rotimg
/
rotimg.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
8KB
|
447 lines
/*
* Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*/
/* rotat image
* Thant Tessman - April, '88
*
*/
#include <stdio.h>
#include <gl.h>
#include <math.h>
#include <device.h>
#include <gl/image.h>
#define X 0
#define Y 1
#define Z 2
short rb[2048];
short gb[2048];
short bb[2048];
unsigned long *colors;
float *grid;
float dist=0.0, d_dist=0.0;
Angle azim=0, inc=0, twist=0;
Angle d_azim=0, d_inc=0;
short mx, my, omx, omy, nmx, nmy;
int origx, origy, sizex, sizey;
short dev, val;
int active=TRUE;
long gid;
int menu;
int low_res, low_tmp;
int function=0;
#define REORIENT 1
#define REDISTANCE 2
float ident_matrix[4][4] = {
{1.0, 0.0, 0.0, 0.0},
{0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0},
{0.0, 0.0, 0.0, 1.0},
};
int y, xsize, ysize;
char *title;
main (argc, argv)
int argc;
char **argv;
{
IMAGE *image;
char *strrchr();
title = (title=strrchr(argv[0], '/')) != NULL ? title+1 : argv[0];
if( argc<2 ) {
fprintf(stderr, "usage: %s infile\n", title);
exit(1);
}
if( (image=iopen(argv[1],"r")) == NULL ) {
fprintf(stderr, "%s: can't open input file %s\n", title, argv[1]);
exit(1);
}
if( image->zsize < 3 ) {
fprintf(stderr, "%s: input image is not a color image\n", title);
exit(1);
}
xsize = image->xsize;
ysize = image->ysize;
low_tmp = low_res = (xsize+ysize)/100+1;
colors = (unsigned long *)malloc(xsize*ysize*sizeof(unsigned long));
grid = (float *)malloc(xsize * ysize * 3 * sizeof(int));
fprintf(stderr, "%s: loading image. Please wait...", title);
initialize();
for(y=0; y<ysize; y++) {
getrow(image, rb, y, 0);
load_colors(rb, colors, xsize, y, 'r');
getrow(image, gb, y, 1);
load_colors(gb, colors, xsize, y, 'g');
getrow(image, bb, y, 2);
load_colors(bb, colors, xsize, y, 'b');
}
fprintf(stderr, "Done!\n");
draw_scene();
while(TRUE) {
while(qtest() || (!function && low_tmp<2)) {
dev=qread(&val);
switch(dev) {
case INPUTCHANGE:
active = val;
if (val);
break;
case ESCKEY :
if (val) break;
case WINQUIT:
gexit();
exit(0);
case LEFTMOUSE:
mx = getvaluator(MOUSEX);
my = getvaluator(MOUSEY);
if (val) {
function=REDISTANCE;
/* these are commented out because of a bug in journaling
* (can't handle setvaluators)
*/
/* setvaluator(MOUSEX, mx, origx+1, origx+sizex-1);
setvaluator(MOUSEY, my, origy+1, origy+sizey-1);
*/
omx = mx; omy = my;
} else {
function=0;
/* setvaluator(MOUSEX, mx, 0, XMAXSCREEN);
setvaluator(MOUSEY, my, 0, YMAXSCREEN);
*/
dist += d_dist; d_dist = 0.0;
draw_scene();
}
break;
case MIDDLEMOUSE:
mx = getvaluator(MOUSEX);
my = getvaluator(MOUSEY);
if (val) {
function=REORIENT;
/* setvaluator(MOUSEX, mx, origx+1, origx+sizex-1);
setvaluator(MOUSEY, my, origy+1, origy+sizey-1);
*/
omx = mx; omy = my;
} else {
function=0;
/* setvaluator(MOUSEX, mx, 0, XMAXSCREEN);
setvaluator(MOUSEY, my, 0, YMAXSCREEN);
*/
azim += d_azim; d_azim = 0;
inc += d_inc; d_inc = 0;
draw_scene();
}
break;
case RIGHTMOUSE:
if (val) {
switch (dopup(menu)) {
case 1:
setup_flat_grid();
break;
case 2:
setup_wavy_grid();
break;
}
low_tmp=low_res;
draw_scene();
}
break;
case REDRAW:
reshapeviewport();
getorigin(&origx, &origy);
getsize(&sizex, &sizey);
draw_scene();
break;
}
}
switch(function) {
case REORIENT:
mx = getvaluator(MOUSEX);
my = getvaluator(MOUSEY);
d_azim = (omx - mx) * 2 * getgdesc(GD_XPMAX) / sizex;
d_inc = (my - omy) * 2 * getgdesc(GD_YPMAX) / sizey;
break;
case REDISTANCE:
mx = getvaluator(MOUSEX);
my = getvaluator(MOUSEY);
d_dist = ((omx - mx) + (my - omy)) * getgdesc(GD_XPMAX) /sizex;
break;
}
draw_scene();
}
}
initialize() {
keepaspect(xsize, ysize);
gid=winopen(title);
if (getgdesc(GD_BITS_NORM_SNG_RED) == 0)
{
system("inform 'Your system must support RGB mode to run rotimg'");
exit(1);
}
if (getgdesc(GD_BITS_NORM_ZBUFFER) == 0)
{
system("inform 'Your system must have a z-buffer to run rotimg'");
exit(1);
}
doublebuffer();
RGBmode();
gconfig();
RGBcolor(0, 0, 0);
clear(); swapbuffers(); clear();
qdevice(INPUTCHANGE);
qdevice(REDRAW);
qdevice(ESCKEY);
qdevice(WINQUIT);
qdevice(LEFTMOUSE);
qdevice(MIDDLEMOUSE);
qdevice(RIGHTMOUSE);
getorigin(&origx, &origy);
getsize(&sizex, &sizey);
zbuffer(TRUE);
lsetdepth(0x00, 0x7fffff);
clear_color_grid();
setup_flat_grid();
dist = ysize/2;
menu = defpup("flat|wavy");
}
clear_color_grid() {
int i, j;
for (j=0; j<ysize; j++) {
for (i=0; i<xsize; i++) {
colors[j*xsize + i] = 0;
}
}
}
setup_wavy_grid() {
int i, j;
float a, b, x, y, r;
r = xsize/4;
for (j=0; j<ysize; j++) {
b = (float) j * 3.14159265359 / ysize;
y = cos(b*3) * r;
for (i=0; i<xsize; i++) {
a = (float) i * 3.14159265359 / xsize;
x = cos(a*2) * r;
grid[3 * (j*xsize + i) + 0] = i - xsize/2;
grid[3 * (j*xsize + i) + 1] = j - ysize/2;
grid[3 * (j*xsize + i) + 2] = x + y;
}
}
}
setup_flat_grid() {
int i, j;
for (j=0; j<ysize; j++) {
for (i=0; i<xsize; i++) {
grid[3 * (j*xsize + i) + 0] = i - xsize/2;
grid[3 * (j*xsize + i) + 1] = j - ysize/2;
grid[3 * (j*xsize + i) + 2] = 0;
}
}
}
draw_grid() {
register int i, j;
register float *gp1, *gp2;
register unsigned long *cp1, *cp2;
for (j=0; (j<ysize-1) && !qtest(); j++) {
gp1 = &grid[3 * j * xsize];
gp2 = &grid[3 * (j+1) * xsize];
cp1 = &colors[j * xsize];
cp2 = &colors[(j+1) * xsize];
bgntmesh();
for (i=0; i<xsize-1; i++) {
cpack(*cp1++);
v3f(gp1+=3);
cpack(*cp2++);
v3f(gp2+=3);
}
endtmesh();
}
}
draw_quick_grid(s)
int s;
{
register int i, j, t;
register float *gp1, *gp2;
register unsigned long *cp1, *cp2;
t = s * 3;
for (j=0; (j<ysize-2) && !qtest(); j+=2) {
gp1 = &grid[3 * j * xsize];
gp2 = &grid[3 * (j+2) * xsize];
cp1 = &colors[j * xsize];
cp2 = &colors[(j+2) * xsize];
bgntmesh();
for (i=0; i<xsize-s; i+=s) {
cp1+=s;
cpack(*cp1);
v3f(gp1 += t);
cp2+=s;
cpack(*cp2);
v3f(gp2 += t);
}
endtmesh();
}
}
draw_scene() {
RGBcolor(0, 0, 0);
clear();
zclear();
perspective(900, (float)xsize/(float)ysize, 1.0, 5000.0);
polarview(dist+d_dist, azim+d_azim, inc+d_inc, twist);
if (function) draw_quick_grid(low_tmp=low_res);
else {
low_tmp = low_tmp/2;
if (low_tmp>1) draw_quick_grid(low_tmp);
else {draw_grid();
low_tmp=0;
}
}
if (!qtest()) swapbuffers();
}
load_colors(row, dest, xsize, y, c)
short *row;
unsigned long *dest;
int xsize, y;
char c;
{
int pos;
pos = xsize * y;
dest = &dest[pos];
switch (c) {
case 'r':
case 'R':
while(xsize--) {
*dest = *dest++ + *row++;
}
break;
case 'g':
case 'G':
while(xsize--) {
*dest = *dest++ + ((*row++) << 8);
}
break;
case 'b':
case 'B':
while(xsize--) {
*dest = *dest++ + ((*row++) << 16);
}
break;
}
}